<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">



  <meta name="description" content="记录数组的所有方法"/>




  <meta name="keywords" content="数组方法" />










  <link rel="alternate" href="/teresa/atom.xml" title="Teresa">




  <link rel="shortcut icon" type="image/x-icon" href="/teresa/favicon.ico?v=2.9.0" />



<link rel="canonical" href="https://littlefaye.gitee.io/teresa/2018/10/22/数组方法/"/>



  <link rel="stylesheet" type="text/css" href="/teresa/lib/fancybox/jquery.fancybox.css" />




  <link rel="stylesheet" type="text/css" href="/teresa/lib/nprogress/nprogress.min.css" />



<link rel="stylesheet" type="text/css" href="/teresa/css/style.css?v=2.9.0" />



  
  <script id="baidu_analytics">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?9b4723f0455add81c1300727d6fe2f0f";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <script id="baidu_push">
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>





  <script src="//cdn1.lncld.net/static/js/3.1.1/av-min.js"></script>
  <script id="leancloud">
    AV.init({
      appId: "q8I1nPpo7445tVF84mvOlAyj-gzGzoHsz",
      appKey: "i9j12HoNltgi5uq4DLvonO0h"
    });
  </script>




<script>
  window.config = {"title":"Teresa","subtitle":null,"description":"about fe","author":"Teresa-哼哼","language":"zh-CN","timezone":null,"url":"https://littlefaye.gitee.io/teresa","root":"/teresa/","permalink":":year/:month/:day/:title/","permalink_defaults":null,"source_dir":"source","public_dir":"public","tag_dir":"tags","archive_dir":"archives","category_dir":"categories","code_dir":"downloads/code","i18n_dir":":lang","skip_render":null,"new_post_name":":title.md","default_layout":"post","titlecase":false,"external_link":true,"filename_case":0,"render_drafts":false,"post_asset_folder":false,"relative_link":false,"future":true,"highlight":{"enable":true,"auto_detect":false,"line_number":true,"tab_replace":null,"first_line_number":"always1"},"default_category":"uncategorized","category_map":null,"tag_map":null,"date_format":"YYYY-MM-DD","time_format":"HH:mm:ss","per_page":10,"pagination_dir":"page","theme":"even","deploy":{"type":"git","repo":"https://gitee.com/littleFaye/Teresa.git"},"ignore":[],"keywords":null,"index_generator":{"per_page":10,"order_by":"-date","path":""},"archive_generator":{"per_page":10,"yearly":true,"monthly":true,"daily":false},"tag_generator":{"per_page":10},"category_generator":{"per_page":10},"marked":{"gfm":true,"pedantic":false,"sanitize":false,"tables":true,"breaks":true,"smartLists":true,"smartypants":true,"modifyAnchors":"","autolink":true},"server":{"port":4000,"log":false,"ip":"0.0.0.0","compress":false,"header":true},"since":2015,"favicon":"/favicon.ico","rss":false,"menu":{"Home":"/","Archives":"/archives/","Tags":"/tags","Categories":"/categories","About":"/about"},"color":"Default","toc":true,"fancybox":true,"pjax":true,"copyright":{"enable":true,"license":"<a rel=\"license\" href=\"http://creativecommons.org/licenses/by-nc/4.0/\" target=\"_blank\">知识共享署名-非商业性使用 4.0 国际许可协议</a>"},"reward":{"enable":false,"qrCode":{"wechat":null,"alipay":null}},"social":{"email":"1748711164@qq.com","stack-overflow":null,"twitter":null,"facebook":null,"linkedin":null,"google":null,"github":"https://github.com/Faye0807","weibo":null,"zhihu":null,"douban":null,"pocket":null,"tumblr":null,"instagram":null},"leancloud":{"app_id":"q8I1nPpo7445tVF84mvOlAyj-gzGzoHsz","app_key":"i9j12HoNltgi5uq4DLvonO0h"},"baidu_analytics":"9b4723f0455add81c1300727d6fe2f0f","baidu_verification":null,"google_analytics":null,"google_verification":null,"disqus_shortname":null,"changyan":{"appid":null,"appkey":null},"livere_datauid":null,"version":"2.9.0"};
</script>

    <title> 数组方法 - Teresa </title>
  </head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/teresa/." class="logo">Teresa</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    
      <a href="/teresa/">
        <li class="mobile-menu-item">
          
          
            首页
          
        </li>
      </a>
    
      <a href="/teresa/archives/">
        <li class="mobile-menu-item">
          
          
            归档
          
        </li>
      </a>
    
      <a href="/teresa/tags">
        <li class="mobile-menu-item">
          
          
            标签
          
        </li>
      </a>
    
      <a href="/teresa/categories">
        <li class="mobile-menu-item">
          
          
            分类
          
        </li>
      </a>
    
      <a href="/teresa/about">
        <li class="mobile-menu-item">
          
          
            关于
          
        </li>
      </a>
    
  </ul>
</nav>

    <div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/teresa/." class="logo">Teresa</a>
</div>

<nav class="site-navbar">
  
    <ul id="menu" class="menu">
      
        <li class="menu-item">
          <a class="menu-item-link" href="/teresa/">
            
            
              首页
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/teresa/archives/">
            
            
              归档
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/teresa/tags">
            
            
              标签
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/teresa/categories">
            
            
              分类
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/teresa/about">
            
            
              关于
            
          </a>
        </li>
      
    </ul>
  
</nav>

      </header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content">
            
  
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          数组方法
        
      </h1>

      <div class="post-meta">
        <span class="post-time">
          2018-10-22
        </span>
        
          <div class="post-category">
            
              <a href="/teresa/categories/JavaScript/">JavaScript</a>
            
          </div>
        
        
        <div class="post-visits"
             data-url="/teresa/2018/10/22/数组方法/"
             data-title="数组方法">
            阅读次数 0
          </div>
        
      </div>
    </header>

    
    
  <div class="post-toc" id="post-toc">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1、堆栈进出"><span class="toc-text">1、堆栈进出</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2、join-“分隔符”-将数组转换为字符串（不影响原数组的值）"><span class="toc-text">2、join(“分隔符”) 将数组转换为字符串（不影响原数组的值）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3、reserve-将数组元素逆向排序（-影响原来的数组-）"><span class="toc-text">3、reserve() 将数组元素逆向排序（ 影响原来的数组 ）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4、sort-将数组升序排序-（-影响原来的数组-默认字母表顺序排序-）"><span class="toc-text">4、sort() 将数组升序排序 （ 影响原来的数组 默认字母表顺序排序 ）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5、concat-拼接数组（-返回新数组，不用影响原来数组-）"><span class="toc-text">5、concat() 拼接数组（ 返回新数组，不用影响原来数组 ）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6、slice-startIndex，-endIndex-返回截取数组的片段-（不用影响原来数组）"><span class="toc-text">6、slice(startIndex， endIndex)  返回截取数组的片段 （不用影响原来数组）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7、splice-开始删除元素的索引（删除包含该值），删除个数，此处开始是要插入的元素-（影响原来的数组）"><span class="toc-text">7、splice(开始删除元素的索引（删除包含该值），删除个数，此处开始是要插入的元素)（影响原来的数组）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8、-toString-将数组转换为字符串"><span class="toc-text">8、 toString()  将数组转换为字符串</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9、forEach-遍历循环-传参为函数"><span class="toc-text">9、forEach( ) 遍历循环 传参为函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10、map-循环遍历-传参为函数"><span class="toc-text">10、map()  循环遍历 传参为函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11、-filter-过滤函数-传参为一个判断函数，若为true则返回"><span class="toc-text">11、 filter() 过滤函数 传参为一个判断函数，若为true则返回</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12、every-和-some-传参为函数-逻辑判断方法-返回true或false"><span class="toc-text">12、every() 和 some() 传参为函数  逻辑判断方法 返回true或false</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#13-、-reduce-和-reduceRight-数组元素组合-生成单值-【例-6-2】"><span class="toc-text">13 、 reduce() 和 reduceRight()  数组元素组合 生成单值 【例 6-2】</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#14、-indexOf-和-lastIndexOf-检索数组是否包含指定值"><span class="toc-text">14、 indexOf() 和 lastIndexOf() 检索数组是否包含指定值</span></a></li></ol>
    </div>
  </div>



    <div class="post-content">
      
        <h2 id="1、堆栈进出"><a href="#1、堆栈进出" class="headerlink" title="1、堆栈进出"></a>1、堆栈进出</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"> ===&gt; unshift() |                               |    ===&gt; pop()</span><br><span class="line">                |          [1,2,3,4]            |    </span><br><span class="line"> &lt;=== shift()   |                               |    &lt;=== push()</span><br><span class="line">                |                               |</span><br><span class="line">  // 方法均改变原数组，若有弹出则返回弹出的值  </span><br><span class="line">                    若是添加则返回添加后的数组长度</span><br></pre></td></tr></table></figure>
<h2 id="2、join-“分隔符”-将数组转换为字符串（不影响原数组的值）"><a href="#2、join-“分隔符”-将数组转换为字符串（不影响原数组的值）" class="headerlink" title="2、join(“分隔符”) 将数组转换为字符串（不影响原数组的值）"></a>2、join(“分隔符”) 将数组转换为字符串（不影响原数组的值）</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arry = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>];</span><br><span class="line">arry.join() <span class="comment">// ===&gt; "1,2,3,4"   // 默认分隔符为 ','</span></span><br></pre></td></tr></table></figure>
<h2 id="3、reserve-将数组元素逆向排序（-影响原来的数组-）"><a href="#3、reserve-将数组元素逆向排序（-影响原来的数组-）" class="headerlink" title="3、reserve() 将数组元素逆向排序（ 影响原来的数组 ）"></a>3、reserve() 将数组元素逆向排序（ 影响原来的数组 ）</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">arry.reverse();</span><br><span class="line">arry              <span class="comment">//  ===&gt;均返回 [4,3,2,1]</span></span><br></pre></td></tr></table></figure>
<h2 id="4、sort-将数组升序排序-（-影响原来的数组-默认字母表顺序排序-）"><a href="#4、sort-将数组升序排序-（-影响原来的数组-默认字母表顺序排序-）" class="headerlink" title="4、sort() 将数组升序排序 （ 影响原来的数组 默认字母表顺序排序 ）"></a>4、sort() 将数组升序排序 （ 影响原来的数组 默认字母表顺序排序 ）</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">arry.sort();</span><br><span class="line"><span class="comment">//  ===&gt; 均返回[1,2,3,4]</span></span><br><span class="line">arry.sort(<span class="function"><span class="keyword">function</span>(<span class="params"> a, b </span>)</span>&#123;</span><br><span class="line"> <span class="keyword">return</span> a - b</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="5、concat-拼接数组（-返回新数组，不用影响原来数组-）"><a href="#5、concat-拼接数组（-返回新数组，不用影响原来数组-）" class="headerlink" title="5、concat() 拼接数组（ 返回新数组，不用影响原来数组 ）"></a>5、concat() 拼接数组（ 返回新数组，不用影响原来数组 ）</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[<span class="number">1</span>,<span class="number">2</span>].concat(<span class="number">3</span>,<span class="number">4</span>)        <span class="comment">// ===&gt; [1,2,3,4]</span></span><br><span class="line">[<span class="number">1</span>,<span class="number">2</span>].concat([<span class="number">3</span>,<span class="number">4</span>])      <span class="comment">// ===&gt; [1,2,3,4]</span></span><br><span class="line">[<span class="number">1</span>,<span class="number">2</span>].concat([<span class="number">3</span>,[<span class="number">4</span>,<span class="number">5</span>]],<span class="number">6</span>)  <span class="comment">// ===&gt; [1,2,3,[4,5],6]</span></span><br></pre></td></tr></table></figure>
<h2 id="6、slice-startIndex，-endIndex-返回截取数组的片段-（不用影响原来数组）"><a href="#6、slice-startIndex，-endIndex-返回截取数组的片段-（不用影响原来数组）" class="headerlink" title="6、slice(startIndex， endIndex)  返回截取数组的片段 （不用影响原来数组）"></a>6、slice(startIndex， endIndex)  返回截取数组的片段 （不用影响原来数组）</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 返回数组 [startIndex,endIndex)</span></span><br><span class="line">arry.slice(<span class="number">0</span>,<span class="number">1</span>)  <span class="comment">// ===&gt; [1]</span></span><br><span class="line">arry             <span class="comment">// ===&gt; [1, 2, 3, 4]</span></span><br></pre></td></tr></table></figure>
<h2 id="7、splice-开始删除元素的索引（删除包含该值），删除个数，此处开始是要插入的元素-（影响原来的数组）"><a href="#7、splice-开始删除元素的索引（删除包含该值），删除个数，此处开始是要插入的元素-（影响原来的数组）" class="headerlink" title="7、splice(开始删除元素的索引（删除包含该值），删除个数，此处开始是要插入的元素)（影响原来的数组）"></a>7、splice(开始删除元素的索引（删除包含该值），删除个数，此处开始是要插入的元素)（影响原来的数组）</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 插入或删除数组元素 （影响原来的数组 并返回删除的元素数组）</span></span><br><span class="line"></span><br><span class="line">arry = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>];</span><br><span class="line">arry.splice(<span class="number">2</span>,<span class="number">2</span>)       <span class="comment">// ===&gt; [3, 4]</span></span><br><span class="line">arry                   <span class="comment">// ===&gt; [1, 2]</span></span><br><span class="line">arry.splice(<span class="number">2</span>,<span class="number">0</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>) <span class="comment">// ===&gt; []</span></span><br><span class="line">arry                   <span class="comment">// ===&gt; [1, 2, 3, 4, 5]</span></span><br></pre></td></tr></table></figure>
<h2 id="8、-toString-将数组转换为字符串"><a href="#8、-toString-将数组转换为字符串" class="headerlink" title="8、 toString()  将数组转换为字符串"></a>8、 toString()  将数组转换为字符串</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> ary2 = [<span class="number">1</span>,<span class="number">2</span>,[<span class="number">3</span>,<span class="number">4</span>]];</span><br><span class="line">ary2.toString() <span class="comment">//  ===&gt; "1,2,3,4"   等效于 join()</span></span><br></pre></td></tr></table></figure>
<h2 id="9、forEach-遍历循环-传参为函数"><a href="#9、forEach-遍历循环-传参为函数" class="headerlink" title="9、forEach( ) 遍历循环 传参为函数"></a>9、forEach( ) 遍历循环 传参为函数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">forEach(<span class="function"><span class="keyword">function</span>(<span class="params"> 数组元素，元素索引，当前数组 </span>)</span>&#123;</span><br><span class="line">  <span class="comment">// 函数主体</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">var</span> data = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</span><br><span class="line">data.forEach(<span class="function"><span class="keyword">function</span>(<span class="params"> v, i, a</span>)</span>&#123;</span><br><span class="line">  a[i] = v + <span class="number">1</span>;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// data  ===&gt; [2,3,4]</span></span><br></pre></td></tr></table></figure>
<h2 id="10、map-循环遍历-传参为函数"><a href="#10、map-循环遍历-传参为函数" class="headerlink" title="10、map()  循环遍历 传参为函数"></a>10、map()  循环遍历 传参为函数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// map必须有一个返回值 所有的返回值组成一个新数组</span></span><br><span class="line">map(<span class="function"><span class="keyword">function</span>(<span class="params">数组元素，元素引号，当前数组</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> xxxx;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">var</span> data = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>];</span><br><span class="line"><span class="keyword">var</span> mapData = data.map(<span class="function"><span class="keyword">function</span>(<span class="params"> v, i, a</span>)</span>&#123;</span><br><span class="line">  a[i] = v + <span class="number">10</span>;</span><br><span class="line"><span class="keyword">return</span> i*i</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// data       ===&gt; [11, 12, 13]</span></span><br><span class="line"><span class="comment">// mapData    ===&gt; [0, 1, 4]</span></span><br></pre></td></tr></table></figure>
<h2 id="11、-filter-过滤函数-传参为一个判断函数，若为true则返回"><a href="#11、-filter-过滤函数-传参为一个判断函数，若为true则返回" class="headerlink" title="11、 filter() 过滤函数 传参为一个判断函数，若为true则返回"></a>11、 filter() 过滤函数 传参为一个判断函数，若为true则返回</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.filter(<span class="function"><span class="keyword">function</span>(<span class="params">x</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> x &gt; <span class="number">3</span>;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">//    b ===&gt; [4,5]</span></span><br><span class="line"><span class="comment">//    a ===&gt; [1,2,3,4,5]</span></span><br></pre></td></tr></table></figure>
<h2 id="12、every-和-some-传参为函数-逻辑判断方法-返回true或false"><a href="#12、every-和-some-传参为函数-逻辑判断方法-返回true或false" class="headerlink" title="12、every() 和 some() 传参为函数  逻辑判断方法 返回true或false"></a>12、every() 和 some() 传参为函数  逻辑判断方法 返回true或false</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// every : 当所有元素满足 传参函数 返回值为true时 every返回true</span></span><br><span class="line"><span class="comment">// some : 当存在一个返回值为true时，即返回true</span></span><br><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.every(<span class="function"><span class="keyword">function</span>(<span class="params">x</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> x &gt; <span class="number">3</span>;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">var</span> c = a.some(<span class="function"><span class="keyword">function</span>(<span class="params">x</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> x &gt; <span class="number">3</span>;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// b ===&gt; false</span></span><br><span class="line"><span class="comment">// c ===&gt; true</span></span><br></pre></td></tr></table></figure>
<h2 id="13-、-reduce-和-reduceRight-数组元素组合-生成单值-【例-6-2】"><a href="#13-、-reduce-和-reduceRight-数组元素组合-生成单值-【例-6-2】" class="headerlink" title="13 、 reduce() 和 reduceRight()  数组元素组合 生成单值 【例 6-2】"></a>13 、 reduce() 和 reduceRight()  数组元素组合 生成单值 【例 6-2】</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// reduce() 传参为函数，[起始值]</span></span><br><span class="line"><span class="comment">// reduceRight() 数组执行函数 从右至左</span></span><br><span class="line"><span class="comment">// 例子中 x为 函数上次的反参</span></span><br><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>];</span><br><span class="line"><span class="keyword">var</span> sum = a.reduce(<span class="function"><span class="keyword">function</span>(<span class="params">x, y</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> x + y;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">//===&gt; 10</span></span><br><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>];</span><br><span class="line"><span class="keyword">var</span> sum = a.reduce(<span class="function"><span class="keyword">function</span>(<span class="params">x, y</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">return</span> x + y;</span><br><span class="line">&#125;, <span class="number">10</span>)</span><br><span class="line"><span class="comment">// ===&gt; 20</span></span><br></pre></td></tr></table></figure>
<h2 id="14、-indexOf-和-lastIndexOf-检索数组是否包含指定值"><a href="#14、-indexOf-和-lastIndexOf-检索数组是否包含指定值" class="headerlink" title="14、 indexOf() 和 lastIndexOf() 检索数组是否包含指定值"></a>14、 indexOf() 和 lastIndexOf() 检索数组是否包含指定值</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 传参为 数组是否包含的值，[ 开始检索的数组索引 默认从头开始 ]</span></span><br><span class="line"><span class="comment">// lastIndexOf() 为反向检索</span></span><br><span class="line"><span class="comment">// 若存在 返回索引值；否则返回 -1</span></span><br><span class="line"><span class="keyword">var</span> a = [<span class="number">3</span>,<span class="number">5</span>,<span class="number">7</span>];</span><br><span class="line">a.indexOf(<span class="number">5</span>);   <span class="comment">//  ===&gt; 1</span></span><br><span class="line">a.indexOf(<span class="number">6</span>);   <span class="comment">//  ===&gt; -1</span></span><br></pre></td></tr></table></figure>
      
    </div>

    
      
      

  <div class="post-copyright">
    <p class="copyright-item">
      <span>原文作者: </span>
      <a href="https://littlefaye.gitee.io/teresa">Teresa-哼哼</a>
    </p>
    <p class="copyright-item">
      <span>原文链接: </span>
      <a href="https://littlefaye.gitee.io/teresa/2018/10/22/数组方法/">https://littlefaye.gitee.io/teresa/2018/10/22/数组方法/</a>
    </p>
    <p class="copyright-item">
      <span>许可协议: </span>
      
      <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/" target="_blank">知识共享署名-非商业性使用 4.0 国际许可协议</a>
    </p>
  </div>



      
      
    

    
      <footer class="post-footer">
        
          <div class="post-tags">
            
              <a href="/teresa/tags/JavaScript/">JavaScript</a>
            
          </div>
        
        
        
  <nav class="post-nav">
    
      <a class="prev" href="/teresa/2018/10/22/字符串方法/">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">字符串方法</span>
        <span class="prev-text nav-mobile">上一篇</span>
      </a>
    
    
      <a class="next" href="/teresa/2018/10/17/你不知道的JSON-stringify/">
        <span class="next-text nav-default">你不知道的JSON.stringify</span>
        <span class="prev-text nav-mobile">下一篇</span>
        <i class="iconfont icon-right"></i>
      </a>
    
  </nav>

      </footer>
    

  </article>


          </div>
          
  <div class="comments" id="comments">
    
  </div>


        </div>
      </main>

      <footer id="footer" class="footer">

  <div class="social-links">
    
      
        
          <a href="mailto:1748711164@qq.com" class="iconfont icon-email" title="email"></a>
        
      
    
      
    
      
    
      
    
      
    
      
    
      
        
          <a href="https://github.com/Faye0807" class="iconfont icon-github" title="github"></a>
        
      
    
      
    
      
    
      
    
      
    
      
    
      
    
    
    
  </div>


<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://hexo.io/">Hexo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/ahonn/hexo-theme-even">Even</a>
  </span>

  <span class="copyright-year">
    
    &copy; 
     
      2015 - 
    
    2018

    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">Teresa-哼哼</span>
  </span>
</div>

      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>

    
  
  

  



    
  



  
  





  
    <script type="text/javascript" src="/teresa/lib/jquery/jquery.min.js"></script>
  

  
    <script type="text/javascript" src="/teresa/lib/slideout/slideout.js"></script>
  

  
    <script type="text/javascript" src="/teresa/lib/fancybox/jquery.fancybox.pack.js"></script>
  

  
    <script type="text/javascript" src="/teresa/lib/pjax/jquery.pjax.min.js"></script>
  

  
    <script type="text/javascript" src="/teresa/lib/nprogress/nprogress.min.js"></script>
  


    <script type="text/javascript" src="/teresa/js/src/even.js?v=2.9.0"></script>

  </body>
</html>
